home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Devices / SCSI Simple Sample / Src / StringFormat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-24  |  4.2 KB  |  232 lines  |  [TEXT/KAHL]

  1. /*                                    StringFormat.c                            */
  2. /*
  3.  * StringFormat.c
  4.  * Copyright © 1993 Apple Computer Inc. All rights reserved.
  5.  *
  6.  * These functions convert values to readable text.
  7.  */
  8. #include <Memory.h>
  9. #include <OSUtils.h>
  10. #pragma segment LogManager
  11.  
  12. #define NUL        '\0'
  13.  
  14. /*
  15.  * AppendChar writes a character into the string. Note that
  16.  * it wraps around if the string size exceeds 255 bytes.
  17.  */
  18. #define AppendChar(result, c) (result[++result[0]] = (c))
  19. void                        AppendUnsigned(
  20.         StringPtr                result,
  21.         unsigned long            value
  22.     );
  23. void                        AppendSigned(
  24.         StringPtr                result,
  25.         signed long                value
  26.     );
  27. void                        AppendUnsignedLeadingZeros(
  28.         StringPtr                result,
  29.         unsigned long            value,
  30.         short                    fieldWidth,
  31.         short                    terminatorChar
  32.     );
  33. void                        AppendUnsignedInField(
  34.         StringPtr                result,
  35.         unsigned long            value,
  36.         short                    fieldWidth
  37.     );
  38. void                        AppendHexLeadingZeros(
  39.         StringPtr                result,
  40.         unsigned long            value,
  41.         short                    fieldWidth
  42.     );
  43. void                        AppendBytes(
  44.         StringPtr                result,
  45.         const Ptr                source,
  46.         unsigned short            length
  47.     );
  48. void                        AppendPascalString(
  49.         StringPtr                result,
  50.         const StringPtr            value
  51.     );
  52. void                        AppendCString(
  53.         StringPtr                result,
  54.         const char                *value,
  55.         unsigned short            maxLength
  56.     );
  57. void                        AppendOSType(
  58.         StringPtr                result,
  59.         OSType                    value
  60.     );
  61.  
  62.  
  63. /*
  64.  * AppendUnsignedLeadingZeros
  65.  * Output an n-digit decimal value with leading zeros.
  66.  */
  67. void
  68. AppendUnsignedLeadingZeros(
  69.         StringPtr            result,
  70.         unsigned long        value,
  71.         short                digits,
  72.         short                terminator
  73.         
  74.     )
  75. {
  76.         if (--digits > 0)
  77.             AppendUnsignedLeadingZeros(result, value / 10, digits, NUL);
  78.         AppendChar(result, (value % 10) + '0');
  79.         if (terminator != NUL)
  80.             AppendChar(result, terminator);
  81. }
  82.  
  83. /*
  84.  * AppendSigned
  85.  * Output a signed decimal longword.
  86.  */
  87. void
  88. AppendSigned(
  89.         StringPtr            result,
  90.         signed long            value
  91.     )
  92. {
  93.         if (value < 0) {
  94.             AppendChar(result, '-');
  95.             value = (-value);
  96.         }
  97.         AppendUnsigned(result, (unsigned long) value);
  98. }
  99.  
  100. /*
  101.  * AppendUnsigned
  102.  * Output an unsigned decimal longword.
  103.  */
  104. void
  105. AppendUnsigned(
  106.         StringPtr            result,
  107.         unsigned long        value
  108.     )
  109. {
  110.         if (value >= 10)
  111.             AppendUnsigned(result, value / 10);
  112.         AppendChar(result, (value % 10) + '0');
  113. }
  114.  
  115. /*
  116.  * Store a number right-justified in a fixed-width field.
  117.  */
  118. void
  119. AppendUnsignedInField(
  120.         StringPtr            result,
  121.         unsigned long        value,
  122.         short                fieldWidth
  123.     )
  124. {
  125.         Str255                work;
  126.         
  127.         work[0] = 0;
  128.         AppendUnsigned(work, value);
  129.         result[0] = 0;
  130.         while (work[0] < fieldWidth) {
  131.             AppendChar(result, ' ');
  132.             --fieldWidth;
  133.         }
  134.         AppendPascalString(result, work);
  135. }
  136.         
  137. /*
  138.  * AppendHexLeadingZeros
  139.  * Output a string of hex digits with leading zeros. May not
  140.  * move memory. Note that "digits" is the field width, not
  141.  * the number of hex bytes.
  142.  */
  143. void
  144. AppendHexLeadingZeros(
  145.         StringPtr            result,
  146.         unsigned long        value,
  147.         short                fieldWidth
  148.     )
  149. {
  150.         if (--fieldWidth > 0)
  151.             AppendHexLeadingZeros(result, value >> 4, fieldWidth);
  152.         value &= 0x0F;
  153.         AppendChar(result,
  154.                 (value < 10)
  155.                 ? value + '0'
  156.                 : (value + ('A' - 10))
  157.             );
  158. }
  159.  
  160. /*
  161.  * AppendOSType
  162.  * Output a 4-byte character string. Unknown bytes (characters
  163.  * below ' ') are replaced by '.'.
  164.  */
  165. void
  166. AppendOSType(
  167.         StringPtr                result,
  168.         OSType                    datum
  169.     )
  170. {
  171.         char                    value[sizeof (OSType)];
  172.         register short            i;
  173.         register unsigned char    c;
  174.         
  175.         BlockMove(&datum, value, sizeof (OSType));
  176.         AppendChar(result, '\'');
  177.         for (i = 0; i < sizeof (OSType); i++) {
  178.             c = value[i];
  179.             if (c < ' ')
  180.                 c = '.';
  181.             AppendChar(result, c);
  182.         }
  183.         AppendChar(result, '\'');
  184. }
  185.  
  186. void
  187. AppendCString(
  188.         StringPtr                result,
  189.         const char                *source,
  190.         unsigned short            maxLength
  191.     )
  192. {
  193.         register unsigned char    *ptr;
  194.         
  195.         ptr = (unsigned char *) source;
  196.         while (*ptr != '\0'
  197.             && (maxLength == 0 || (ptr - source) < maxLength))
  198.             AppendChar(result, *ptr++);
  199. }
  200.  
  201. /*
  202.  * Append a fixed-sized string.
  203.  */
  204. void
  205. AppendBytes(
  206.         StringPtr                result,
  207.         const Ptr                source,
  208.         unsigned short            length
  209.     )
  210. {
  211.         register unsigned char    *ptr;
  212.         
  213.         ptr = (unsigned char *) source;
  214.         while (length-- > 0)
  215.             AppendChar(result, *ptr++);
  216. }
  217.  
  218. /*
  219.  * AppendPascalString
  220.  */
  221. void
  222. AppendPascalString(
  223.         StringPtr                result,
  224.         const StringPtr            datum
  225.     )
  226. {
  227.         register short            i;
  228.         
  229.         for (i = 1; i <= datum[0]; i++)
  230.             AppendChar(result, datum[i]);
  231. }
  232.